<template>
  <div id="editTaskItemDialog">
    <el-dialog :visible.sync="visible" :title="title" width="600px"
               @closed='closedDialog'
               @open="openDialog">
      <SimilarTaskItemTableDialog ref="similarTaskItemTableDialog"
                                  :containsSimilarTaskItemList="containsSimilarTaskItemList"
                                  :knowledgeLibId="knowledgeLibId"
                                  @quit="quit" @update="update" @create="create" @close="closeSimilarTaskTableDialog"/>
      <el-row>
        <el-col :span="6">
          <span class="label">子任务名称<em style="color: red">*</em></span>
        </el-col>
        <el-col :span="18">
          <!-- 修改子任务, 放开修改名称限制, 但是不做名称重复性校验 -->
          <KindEditor :content.sync="taskItemBak.name" :readonly='taskItemBak.status === 8' editor-type="Input"/>
        </el-col>
      </el-row>
      <el-row style="margin-top: 10px">
        <el-col :span="6">
          <span class="label">编辑</span>
        </el-col>
        <el-col :span="18">
          <el-select filterable v-model="taskItemBak.editor" style="width: 100%"
                     placeholder="请选择" :disabled="title === '新建知识'">
            <el-option
              v-for="(item,index) in editorList"
              :key="index"
              :label="item.name + ' ' + item.roleNames"
              :value="item.userName">
            </el-option>
          </el-select>
        </el-col>
      </el-row>

      <el-row style="margin-top: 10px" v-if="auditLevel > 0">
        <el-col :span="6">
          <span class="label">初审编辑</span>
        </el-col>
        <el-col :span="18">
          <el-select filterable v-model="taskItemBak.firstAuditor" style="width: 100%"
                     placeholder="请选择">
            <el-option
              v-for="(item,index) in auditorList"
              :key="index"
              :label="item.name + ' ' + item.roleNames"
              :value="item.userName">
            </el-option>
          </el-select>
        </el-col>
      </el-row>

      <el-row style="margin-top: 10px" v-if="auditLevel > 1">
        <el-col :span="6">
          <span class="label">复审编辑</span>
        </el-col>
        <el-col :span="18">
          <el-select filterable v-model="taskItemBak.secondAuditor" style="width: 100%"
                     placeholder="请选择">
            <el-option
              v-for="(item,index) in auditorList"
              :key="index"
              :label="item.name + ' ' + item.roleNames"
              :value="item.userName">
            </el-option>
          </el-select>
        </el-col>
      </el-row>

      <el-row style="margin-top: 10px" v-if="auditLevel > 2">
        <el-col :span="6">
          <span class="label">终审编辑</span>
        </el-col>
        <el-col :span="18">
          <el-select filterable v-model="taskItemBak.thirdAuditor" style="width: 100%"
                     placeholder="请选择">
            <el-option
              v-for="(item,index) in auditorList"
              :key="index"
              :label="item.name + ' ' + item.roleNames"
              :value="item.userName">
            </el-option>
          </el-select>
        </el-col>
      </el-row>

      <el-row style="margin-top: 10px">
        <el-col :span="6">
          <span class="label">备注</span>
        </el-col>
        <el-col :span="18">
          <el-input maxlength="100" show-word-limit
                    v-model.trim="taskItemBak.remark"/>
        </el-col>
      </el-row>

      <span class="dialog-footer" slot="footer">
        <el-button @click="closedDialog">取 消</el-button>
        <el-button v-if="title === '修改子任务'" @click="updateTaskItemById" type="primary">确 定</el-button>
        <el-button v-else @click="selectSimilarTaskItem" type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import SimilarTaskItemTableDialog from "./SimilarTaskItemTableDialog";

export default {
  name: "EditTaskItemDialog",
  components: {
    SimilarTaskItemTableDialog,
  },
  props: ['auditLevel', 'knowledgeLibId'],
  data() {
    return {
      title: '',
      visible: false,
      taskItem: {},
      taskItemBak: {},
      containsSimilarTaskItemList: []
    }
  },
  computed: {
    editorList() {
      return this.$store.state.userList
    },
    auditorList() {
      return this.$store.state.userList
    },
  },
  methods: {
    quit() {
      this.containsSimilarTaskItemList = []
      this.visible = false
    },
    create() {
      this.containsSimilarTaskItemList = []
      this.updateTaskItemById()
    },
    update(taskItemWithSimilarList, similarTaskItem) {
      this.containsSimilarTaskItemList = []
      this.taskItemBak.name = similarTaskItem.name
      this.taskItemBak.knowledgeId = similarTaskItem.knowledgeId
      this.taskItemBak.type = CONST.TASK_ITEM.TYPE_EDIT
      this.taskItemBak.wordsCount = similarTaskItem.wordsCount
      this.updateTaskItemById()
    },
    closeSimilarTaskTableDialog() {
      this.containsSimilarTaskItemList = []
    },
    openDialog() {
      this.taskItemBak = JSON.parse(JSON.stringify(this.taskItem))
    },
    closedDialog() {
      this.visible = false
    },
    selectSimilarTaskItem() {
      if (!this.taskItemBak.name) {
        this.$message.warning("子任务名称不能为空")
        return
      }
      this.axios.get('/pmph/action/task/selectSimilarTaskItem', {
        params: {
          taskItemName: this.taskItemBak.name,
          knowledgeLibId: this.knowledgeLibId
        }
      })
        .then((response) => {
          if (response.data.length == 0) {
            this.updateTaskItemById()
          } else {
            this.taskItemBak.similarTaskItemList = response.data
            this.containsSimilarTaskItemList.push(this.taskItemBak)
            this.$refs.similarTaskItemTableDialog.visible = true
          }
        })
    },
    updateTaskItemById() {
      let param = new URLSearchParams();
      param.append("taskItem", JSON.stringify(this.taskItemBak))
      // 如果是新增的任务, 数据库没有相关子任务, 无需同步后台;
      if (this.taskItem.taskId) {
        this.axios.post('/pmph/action/task/saveTaskItem', param)
          .then((response) => {
            this.taskItem.knowledgeId = this.taskItemBak.knowledgeId
            this.taskItem.name = this.taskItemBak.name
            this.taskItem.editor = this.taskItemBak.editor
            this.taskItem.firstAuditor = this.taskItemBak.firstAuditor
            this.taskItem.secondAuditor = this.taskItemBak.secondAuditor
            this.taskItem.thirdAuditor = this.taskItemBak.thirdAuditor
            this.taskItem.remark = this.taskItemBak.remark
            this.visible = false
            if (!this.taskItem.id) {
              this.taskItem.id = response.data.id
              this.taskItem.type = response.data.type
              this.taskItem.wordsCount = response.data.wordsCount
              this.$emit('insertTaskItem', this.taskItem)
            }
          })
      } else {
        this.visible = false
        if (this.title === '修改子任务') {
          this.taskItem.knowledgeId = this.taskItemBak.knowledgeId
          this.taskItem.name = this.taskItemBak.name
          this.taskItem.editor = this.taskItemBak.editor
          this.taskItem.firstAuditor = this.taskItemBak.firstAuditor
          this.taskItem.secondAuditor = this.taskItemBak.secondAuditor
          this.taskItem.thirdAuditor = this.taskItemBak.thirdAuditor
          this.taskItem.remark = this.taskItemBak.remark
        } else {
          this.$emit('insertTaskItem', this.taskItemBak)
        }
      }
    }
  }
}
</script>

<style scoped>
.el-row {
  line-height: 40px;
}

.el-col {
  display: inline-block;
}
</style>
